package de.uni.muenster.idw2;

import java.util.ArrayList;
import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import de.uni.muenster.idw2.adapter.BuildingGalleryAdapter;
import de.uni.muenster.idw2.data.Building;
import de.uni.muenster.idw2.threads.GridLoaderThread;
import de.uni.muenster.idw2.threads.ImageLoaderThradBuildingDialog;
import de.uni.muenster.idw2.view.BuildingGalleryView;

public class BuildingInformation extends Activity{
	
	private Building building;
	
	private TextView textViewDesc;
	private TextView textViewName;
	private ImageView imageView;
	private ProgressBar progressBar;
	private BuildingGalleryView buildingGallery;
	private LinearLayout layoutGallery;

	private ImageLoaderThradBuildingDialog imgLoader = null;

	private ArrayList<String> paths = new ArrayList<String>();

	private GridLoaderThread gridLoaderThread;

	private LinkedList<Drawable> drawables = new LinkedList<Drawable>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.building_information);
		
		building = Building.getBuilding();
		
		textViewDesc = (TextView) findViewById(R.id.textViewBuildingDesc);
		textViewName = (TextView) findViewById(R.id.textViewBuildingName);
		imageView = (ImageView) findViewById(R.id.imageViewBuilding);
		progressBar = (ProgressBar) findViewById(R.id.title_refresh_progress);
		buildingGallery = (BuildingGalleryView) findViewById(R.id.galleryBuilding);
		layoutGallery = (LinearLayout) findViewById(R.id.galleryLayout);
		
		
		
		int hight = getResources().getDrawable(R.drawable.icon).getMinimumHeight();
        int width = getResources().getDrawable(R.drawable.icon).getMinimumWidth();
        
        double diff = width/((double)hight);
        
        Display display = getWindowManager().getDefaultDisplay(); 
        int displayWidth =(int) (display.getWidth() / 4);
        
        imageView.setLayoutParams(new LinearLayout.LayoutParams((int)(displayWidth * diff), displayWidth));
		
		if(building != null){
			paths = building.getImages();
			textViewName.setText(building.getName());
			textViewDesc.setText(building.getDescription());
		}
		
		
		BuildingGalleryAdapter adapter = new BuildingGalleryAdapter(this, paths);
		buildingGallery.setAdapter(adapter);
		
		gridLoaderThread = new GridLoaderThread(this, progressBar, adapter, drawables , paths, buildingGallery, imageView);
		gridLoaderThread.execute();
		
		buildingGallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				
			}
		});
		
	}
	
	
	public void map(View view){
		finish();
	}
	
	public void navigate(View view){
		if(building != null){
			Intent intent = new Intent(this, RouteSelector.class);
	    	intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	    	startActivity(intent);
			}
		else{
			Toast.makeText(this, "NO USEFUL BUILDING INFORMATION", 4).show();
		}
		finish();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		if (imgLoader != null && !imgLoader.isCancelled()) {
			imgLoader.cancel(true);
		}
	}
}
